<%@ page language="Java"%>
<%@ page contentType="text/html; charset=UTF8"%> 
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="todo.model.Todo" %>
<%@ page import="todo.Utils" %>
<%@ page import="todo.dao.Dao" %>

<!DOCTYPE html>


<%@page import="java.util.ArrayList"%>

<html lang="pl-PL">
	<head>
		<title>Notes</title>
		<link rel="stylesheet" type="text/css" href="css/main.css"/>
		<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
		
		<!-- TinyMCE -->
		<script type="text/javascript" src="/js/tinymce/tiny_mce.js"></script>
		<script type="text/javascript">
			tinyMCE.init({
				// General options
				mode : "textareas",
				theme : "advanced",
				plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
		
				// Theme options
				theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
		
				// Example content CSS (should be your site CSS)
				content_css : "css/content.css",
		
				// Drop lists for link/image/media/template dialogs
				template_external_list_url : "lists/template_list.js",
				external_link_list_url : "lists/link_list.js",
				external_image_list_url : "lists/image_list.js",
				media_external_list_url : "lists/media_list.js",
		
				// Style formats
				style_formats : [
					{title : 'Bold text', inline : 'b'},
					{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
					{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
					{title : 'Example 1', inline : 'span', classes : 'example1'},
					{title : 'Example 2', inline : 'span', classes : 'example2'},
					{title : 'Table styles'},
					{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
				],
		
				formats : {
					alignleft : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'left'},
					aligncenter : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'center'},
					alignright : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'right'},
					alignfull : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'full'},
					bold : {inline : 'span', 'classes' : 'bold'},
					italic : {inline : 'span', 'classes' : 'italic'},
					underline : {inline : 'span', 'classes' : 'underline', exact : true},
					strikethrough : {inline : 'del'}
				},
		
				// Replace values for the template plugin
				template_replace_values : {
					username : "Some User",
					staffid : "991234"
				}
			});
		</script>
		
	</head>
	
	<body>
<%
Dao dao = Dao.INSTANCE;

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();

String url = userService.createLoginURL(request.getRequestURI());
String urlLinktext = "Zaloguj";
List<Todo> todos = new ArrayList<Todo>();
Todo edited = null;
String pageId = request.getParameter("pageId");
String viewId = request.getParameter("viewId");
String search = request.getParameter("search");
if (search != null && !search.equals(""))
{
	todos = dao.searchTodos(search, pageId);
}
else
{
	todos = dao.getTodos(pageId, viewId);
}
boolean isMe = false;
if (user != null)
{
    url = userService.createLogoutURL(request.getRequestURI());
    urlLinktext = "Wyloguj";
    
    String editId = request.getParameter("editid");
    if (editId != null) {edited = dao.getTodo(editId);}
    isMe = user.getNickname().equals(System.getProperty("nickname")) || Utils.isDevMode();
}
%>

<div id="main">

	<div>
		<div>
			<div style="float: left;">
				<a href="index.jsp"><img src="images/home.png" height="35" width="35"/></a>
			</div>
			<div style="float: right;">
				<a href="<%=url%>"><img src="images/login.png" height="30" width="30"/></a>
			</div>
		</div>
	</div>
	
<div> <br/><br/> </div>

<div id="dataentry">
<% if (user != null){ %> 
	<% if (isMe || Utils.isDevMode()) { %>
		<form action="/new" method="post" accept-charset="utf-8">
			<input type="hidden" value="<%=edited!=null?edited.getId():""%>" name="editId" id="editId"/>
			<br/><a>Tytuł:</a>
			<br/>
			<input type="text" id="title" value="<%=edited!=null ? edited.getShortDescription() : "" %>" name="summary" id="summary" size="65"/>
			<br/>
			<textarea rows="4" id="content" cols="90" name="description" id="description"><%=edited!=null ? edited.getLongDescription() : "" %></textarea>
			<br/>
			<input type="submit" value="Zapisz"/>   <input type="button" name="Cancel" value="Anuluj" onclick="window.location = '<%=request.getRequestURI()%>' " />
			<br/><br/><br/>
			
		</form>
	<% }  else 
	{ // not pmaslany %>
		<a> Niestety nie jesteś uprawniony do edycji danych... </a>
	<% } %>
	
<% } // end if user != null %> 

<div>
	<form action="/search" method="post" accept-charset="utf-8">
		<a>Szukaj według:</a>
		<input type="text" name="search" id="search" size="65"/>
		<input type="submit" value="Szukaj"/>
	</form>
</div>

</div>

<div> <br/> </div>

<div>
	<div>
		<% if (todos.size() > 0 && pageId != null && Long.parseLong(pageId) > 0 ) { %>
			<div style="float: left;">
				<a href="<%=request.getRequestURI() + "?pageId=" + (Long.parseLong(pageId) - 1) %>"> <img src="images/prevPage.png" height="30" width="30"/> </a> 
			</div>
		<% } %>
		<% if ( todos.size() == 10 ) { %>
		<div style="float: right;">
			<a href="<%=request.getRequestURI() + "?pageId=" + (pageId == null ? 1 : (Long.parseLong(pageId) + 1)) %>"> <img src="images/nextPage.png" height="30" width="30"/> </a> 
		</div>
		<% } %>
	</div>
</div>

<div> <br/><br/> </div>

<table>
<% for (Todo todo : todos) {%>
	<tr>
      <th>
      	<% if (isMe) {%>
		    <p style="float: left; font-size: x-small;">
				<%="<a style=\"text-align:left;\" href=\"" + request.getRequestURI() + "?editid="+todo.getId() + "\"> <img src=\"images/edit.png\" height=\"20\" width=\"20\"/> </a>"%>
			</p>
		<%} %>
		
      	<a href="<%=request.getRequestURI() + "?viewId=" + todo.getId() %>"> <%=todo.getShortDescription().toUpperCase()%> </a>
      	
      	<% if (isMe) {%>
		    <p style="float: right; font-size: x-small;">
				<%="<a style=\"text-align:right;\" href=\"/done?id="+todo.getId() + "\"> <img src=\"images/delete.png\" height=\"20\" width=\"20\"/> </a>"%>
			</p>
		<%} %>
      </th>
    </tr>
	
	<tr> 
		<td>
		<%=todo.getLongDescription()%>
		</td>
	</tr> 

	<tr/><tr/>
			
<%} //end for todo%>
</table>
<div>
	<div>
		<% if (todos.size() > 0 && pageId != null && Long.parseLong(pageId) > 0 ) { %>
			<div style="float: left;">
				<a href="<%=request.getRequestURI() + "?pageId=" + (Long.parseLong(pageId) - 1) %>"> <img src="images/prevPage.png" height="30" width="30"/> </a> 
			</div>
		<% } %>
		<% if ( todos.size() == 10 ) { %>
		<div style="float: right;">
			<a href="<%=request.getRequestURI() + "?pageId=" + (pageId == null ? 1 : (Long.parseLong(pageId) + 1)) %>"> <img src="images/nextPage.png" height="30" width="30"/> </a> 
		</div>
		<% } %>
	</div>
</div>

</div> <!-- div main -->

<div id="footer">
	<a href="mailto:axytaxyt[at]gmail.com"> Strona hostowana na google appengine </a>
</div>

</body>
</html>